package com.zephyr.nio;

import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;

public class Buffer {

    @Test
    public void test() {
        //1.分配指定大小的缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        show(buffer);

        //2.利用put()存入数据到缓冲区
        buffer.put("abcde".getBytes());
        show(buffer);

        //3.切换读模式
        buffer.flip();
        show(buffer);

        //4.利用get()读取缓冲区中的数据
        byte[] dst = new byte[buffer.limit()];
        buffer.get(dst);
        System.out.println(new String(dst, 0, dst.length));
        show(buffer);
    }

    private void show(ByteBuffer buffer) {
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("----------------------------------------");
    }

}
